/* eslint-disable no-sparse-arrays */
<template>
	<el-dialog
		:title="title"
		:visible.sync="addComShow"
		width="80%"
		:before-close="cancel"
		:close-on-click-modal="false"
		:close-on-press-escape="false"
		style="margin-top: -8vh"
	>
		<el-form ref="form" label-width="140px">
			<el-form-item label="基金经理名称" required>
				<el-select
					:disabled="title === '查看' || title === '修改'"
					filterable
					v-model="fundManagerForm.oaId"
					placeholder="请选择基金经理"
					@change="changeFunc"
				>
					<el-option :label="item.firstName" :value="item.username" v-for="(item, index) in userList" :key="index + 'b'"></el-option>
				</el-select>
			</el-form-item>

			<el-form-item label="部门">
				<el-input :disabled="title === '查看' || title === '修改'" readonly v-model="fundManagerForm.departmentName"></el-input>
			</el-form-item>

			<el-form-item label="职务">
				<el-select :disabled="title === '查看'" multiple v-model="jobList" placeholder="请选择职务">
					<el-option :label="item.positionName" :value="item.positionId" v-for="(item, index) in PostList" :key="index + 'a'"></el-option>
				</el-select>
				<el-button
					icon="el-icon-plus"
					circle
					style="margin-left: 10px; border-radius: 48% !important"
					:disabled="title === '查看'"
					@click="addPostClick"
				></el-button>
			</el-form-item>

			<el-form-item label="性别">
				<div>
					<el-radio-group :disabled="title === '查看'" v-model="fundManagerForm.gender">
						<el-radio label="0">男</el-radio>
						<el-radio label="1">女</el-radio>
					</el-radio-group>
				</div>
			</el-form-item>

			<el-form-item label="类型" required>
				<div>
					<el-radio-group :disabled="['查看', '修改'].includes(title)" v-model="fundManagerForm.managerType">
						<el-radio label="1">基金经理</el-radio>
						<el-radio label="2">投资经理</el-radio>
					</el-radio-group>
				</div>
			</el-form-item>

			<el-form-item label="基金经理类型">
				<el-select :disabled="title === '查看'" v-model="fundManagerForm.fundManagerType" placeholder="请选择基金经理类型">
					<el-option v-for="(item, index) in fundType" :key="index + 'c'" :label="item.typeName" :value="item.typeCode" />
				</el-select>
			</el-form-item>

			<el-form-item label="出生日期">
				<el-date-picker
					:disabled="title === '查看'"
					v-model="fundManagerForm.birthYear"
					type="date"
					value-format="yyyy-MM-dd"
					format="yyyy-MM-dd"
					clearable
					placeholder="选择日期"
				>
				</el-date-picker>
			</el-form-item>

			<el-form-item label="邮箱">
				<el-input :disabled="title === '查看'" v-model="fundManagerForm.email"></el-input>
			</el-form-item>

			<el-form-item label="证件类型">
				<el-select :disabled="title === '查看'" v-model="fundManagerForm.idType" placeholder="请选择证件类型">
					<el-option v-for="(item, index) in idTypeList" :key="index + 'c'" :label="item.typeName" :value="item.typeCode" />
				</el-select>
			</el-form-item>

			<el-form-item label="证件号">
				<el-input :disabled="title === '查看'" v-model="fundManagerForm.idNo"></el-input>
			</el-form-item>

			<el-form-item label="证件到期日">
				<el-date-picker
					:disabled="title === '查看'"
					v-model="fundManagerForm.idEndDate"
					type="date"
					clearable
					value-format="yyyy-MM-dd"
					format="yyyy-MM-dd"
					placeholder="选择日期"
				>
				</el-date-picker>
			</el-form-item>

			<el-form-item label="座机">
				<el-input :disabled="title === '查看'" v-model="fundManagerForm.phone"></el-input>
			</el-form-item>

			<el-form-item label="手机">
				<el-input :disabled="title === '查看'" v-model="fundManagerForm.mobile"></el-input>
			</el-form-item>

			<el-form-item label="学历">
				<el-input :disabled="title === '查看'" v-model="fundManagerForm.education"></el-input>
			</el-form-item>

			<el-form-item label="入职日期">
				<el-date-picker
					:disabled="title === '查看'"
					v-model="fundManagerForm.entryYear"
					type="date"
					value-format="yyyy-MM-dd"
					format="yyyy-MM-dd"
					clearable
					placeholder="选择日期"
				>
				</el-date-picker>
			</el-form-item>

			<el-form-item label="离职日期">
				<el-date-picker
					:disabled="title === '查看'"
					v-model="fundManagerForm.leaveDate"
					type="date"
					value-format="yyyy-MM-dd"
					format="yyyy-MM-dd"
					clearable
					placeholder="选择日期"
				>
				</el-date-picker>
			</el-form-item>

			<el-form-item label="行业从业年限">
				<el-input :disabled="title === '查看'" v-model="fundManagerForm.yearsOfEmployment"></el-input>
			</el-form-item>

			<el-form-item label="证券从业起始日">
				<el-date-picker
					:disabled="title === '查看'"
					v-model="fundManagerForm.stockEntryStartDate"
					type="date"
					value-format="yyyy-MM-dd"
					format="yyyy-MM-dd"
					clearable
					placeholder="选择日期"
				>
				</el-date-picker>
			</el-form-item>

			<el-form-item label="担任基金经理日期">
				<el-date-picker
					:disabled="title === '查看'"
					v-model="fundManagerForm.entryFundManagerDate"
					type="date"
					value-format="yyyy-MM-dd"
					format="yyyy-MM-dd"
					clearable
					placeholder="选择日期"
				>
				</el-date-picker>
			</el-form-item>

			<el-form-item label="担任投资经理日期">
				<el-date-picker
					:disabled="title === '查看'"
					v-model="fundManagerForm.entryAccountManagerDate"
					type="date"
					value-format="yyyy-MM-dd"
					format="yyyy-MM-dd"
					clearable
					placeholder="选择日期"
				>
				</el-date-picker>
			</el-form-item>

			<el-form-item label="担任研究员日期">
				<el-date-picker
					:disabled="title === '查看'"
					v-model="fundManagerForm.entryResearcherDate"
					type="date"
					value-format="yyyy-MM-dd"
					format="yyyy-MM-dd"
					clearable
					placeholder="选择日期"
				>
				</el-date-picker>
			</el-form-item>

			<el-form-item label="个人简历">
				<el-input type="textarea" :disabled="title === '查看'" v-model="fundManagerForm.resume"></el-input>
			</el-form-item>

			<el-form-item label="备注">
				<el-input type="textarea" :disabled="title === '查看'" v-model="fundManagerForm.remark"></el-input>
			</el-form-item>
		</el-form>
		<span slot="footer" class="dialog-footer">
			<el-button @click="cancel" size="small">取 消</el-button>
			<el-button type="primary" size="small" @click="addConfirm" :disabled="['查看'].includes(title)">确 定</el-button>
		</span>
	</el-dialog>
</template>
<script>
import { getPost, addPost, saveManager, departmentUser } from '@/api/productApi/fundManager'
import { timerConversion } from '@/utils/timer'
export default {
	props: {
		title: {
			type: String,
			default: ''
		},
		addComShow: {
			type: Boolean,
			default: false
		},
		comIsShow: {
			type: Function,
			default: Function
		},
		fundType: {
			type: Array,
			default: Array
		},
		departmentList: {
			type: Array,
			default: Array
		},
		idTypeList: {
			type: Array,
			default: Array
		},
		queryFundManagerList: {
			type: Function,
			default: Function
		}
	},
	data() {
		return {
			fundManagerForm: {
				departmentId: ''
			},
			jobList: [],
			PostList: [],
			userList: [],
			timerList: [
				'birthYear',
				'idEndDate',
				'entryYear',
				'leaveDate',
				'entryFundManagerDate',
				'entryAccountManagerDate',
				'entryResearcherDate',
				'stockEntryStartDate'
			]
		}
	},
	mounted() {
		this.getAddData()
	},
	methods: {
		changeFunc() {
			const obj = this.userList.filter(item => item.username === this.fundManagerForm.oaId)
			if (obj.length) {
				const data = this.departmentList.filter(item => item.id === obj[0].departmentId)
				this.fundManagerForm.departmentId = data[0].id ? data[0].id : ''
				this.fundManagerForm.departmentName = data[0].departmentName ? data[0].departmentName : ''
			}
		},
		cancel() {
			this.$emit('comIsShow')
			this.fundManagerForm = {}
			this.jobList = []
		},
		// 新增或编辑
		addConfirm() {
			if (!this.fundManagerForm.oaId || !this.fundManagerForm.managerType) {
				this.$message({ type: 'warning', customClass: 'el-messageClass', message: '请填写必填字段' })
				return
			}
			const data = timerConversion(2, JSON.parse(JSON.stringify(this.fundManagerForm)), this.timerList)
			data.job = [...this.jobList].toString()
			const listKey = Object.keys(data)
			listKey.forEach(item => {
				data[item] = data[item] ? data[item] : ''
			})
			saveManager(data).then(res => {
				if (res.msgCode === 'Y') {
					this.$message({ type: 'success', customClass: 'el-messageClass', message: res.msg })
					this.$emit('queryFundManagerList')
					this.cancel()
				}
			})
		},
		edit(obj) {
			this.fundManagerForm = timerConversion(1, JSON.parse(JSON.stringify(obj)), this.timerList)
			this.jobList = this.fundManagerForm.job ? this.fundManagerForm.job.split(',') : []
			this.$forceUpdate(this.fundManagerForm)
			this.$nextTick(() => {
				this.changeFunc()
			})
		},
		addPostClick() {
			this.$prompt('请输入职务名称', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				inputPattern: /[^\\s\ \r\\n]/,
				inputErrorMessage: '请输入职务名称'
			})
				.then(({ value }) => {
					addPost({ positionName: value }).then(res => {
						if (res.msgCode === 'Y') {
							this.getAddData()
							this.$message({ type: 'success', customClass: 'el-messageClass', message: res.msg })
						}
					})
				})
				.catch(() => {})
		},
		getAddData() {
			getPost().then(res => {
				if (res.msgCode === 'Y') {
					this.PostList = res.data.body
				}
			})
			departmentUser().then(res => {
				if (res.msgCode === 'Y') {
					this.userList = res.data.body
				}
			})
		}
	}
}
</script>
<style lang="scss" scoped>
.el-select,
.el-date-editor {
	width: 100%;
}
::v-deep .el-form-item__content {
	display: flex;
}
</style>

